<template>
  <div class="app-container">
    <div class="custom_card_0">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="活动类型" prop="activityType">
          <el-select
            v-model="queryParams.activityType"
            placeholder="请选择活动类型"
            clearable
            size="small"
          >
            <template v-for="dict in dict.type.activityType">
              <el-option
                :label="dict.label"
                :value="dict.value"
                :key="dict.value"
              ></el-option>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="活动模板" prop="templateDictType">
          <el-select
            v-model="queryParams.templateDictType"
            placeholder="请选择活动模板"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="dict in dict.type.coupon_template"
              :label="dict.label"
              :value="parseInt(dict.value)"
              :key="dict.value"
            ></el-option>
            <el-option
              v-for="dict in dict.type.model_template"
              :label="dict.label"
              :value="parseInt(dict.value)"
              :key="dict.value"
            ></el-option>
            <el-option
              v-for="dict in dict.type.holiday_coupon_template"
              :label="dict.label"
              :value="parseInt(dict.value)"
              :key="dict.value"
            ></el-option>
            <el-option
              v-for="dict in dict.type.holiday_model_template"
              :label="dict.label"
              :value="parseInt(dict.value)"
              :key="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动名称" prop="activityName">
          <el-input
            v-model="queryParams.activityName"
            clearable
            placeholder="请输入活动名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="活动状态" prop="activityStatus">
          <el-select
            v-model="queryParams.activityStatus"
            placeholder="请选择活动状态"
            clearable
            size="small"
          >
            <template v-for="dict in dict.type.activity_status">
              <el-option
                :label="dict.label"
                :value="dict.value"
                :key="dict.value"
              ></el-option>
            </template>
          </el-select>
        </el-form-item>

        <el-form-item label="活动时间" prop="startTime">
          <el-date-picker
            clearable
            size="small"
            v-model="time"
            type="daterange"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="custom_card">
      <el-row :gutter="10" class="mb10">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            :loading="exportLoading"
            @click="handleExport"
            v-hasPermi="['activityManage:activityManage:export']"
            >导出
          </el-button>
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="activityManageList" border>
        <el-table-column
          label="操作"
          align="center"
          width="240"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="{ row }">
            <el-button
              size="mini"
              type="text"
              @click="getInfo(row)"
              v-hasPermi="['activityManage:activityManage:query']"
              >详情</el-button
            >
            <el-dropdown
              size="mini"
              @command="(command) => handleCommand(command, row)"
              v-hasPermi="[
                'activityManage:activityManage:edit',
                'activityManage:activityManage:remove',
                'activityManage:activityManage:suspend',
                'activityManage:activityManage:recovery',
                'activityManage:activityManage:push',
                'activityManage:activityManage:over',
                'activityManage:activityManage:activityData',
              ]"
            >
              <span class="el-dropdown-link">
                <i class="el-icon-d-arrow-right el-icon--right"></i>更多</span
              >
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item
                  command="handleUpdate"
                  icon="el-icon-search"
                  v-hasPermi="['activityManage:activityManage:edit']"
                  v-if="row.activityStatus == 1"
                  >编辑</el-dropdown-item
                >
                <el-dropdown-item
                  command="handleDelete"
                  icon="el-icon-search"
                  v-hasPermi="['activityManage:activityManage:remove']"
                  v-if="row.activityStatus == 1"
                  >删除</el-dropdown-item
                >
                <el-dropdown-item
                  command="handleStart"
                  icon="el-icon-search"
                  v-hasPermi="['activityManage:activityManage:edit']"
                  v-if="row.activityStatus == 1"
                  >开始</el-dropdown-item
                >
                <el-dropdown-item
                  command="handleSuspend"
                  icon="el-icon-search"
                  v-hasPermi="['activityManage:activityManage:suspend']"
                  v-if="row.activityStatus == 2"
                  >暂停</el-dropdown-item
                >
                <el-dropdown-item
                  command="handleRecovery"
                  icon="el-icon-search"
                  v-hasPermi="['activityManage:activityManage:recovery']"
                  v-if="row.activityStatus == 3"
                  >恢复</el-dropdown-item
                >
                <el-dropdown-item
                  command="handlePush"
                  icon="el-icon-search"
                  v-hasPermi="['activityManage:activityManage:push']"
                  v-if="row.activityStatus == 2 && row.isAccurateDelivery == 1"
                  >精准推送</el-dropdown-item
                >
                <el-dropdown-item
                  command="handleOver"
                  icon="el-icon-search"
                  v-hasPermi="['activityManage:activityManage:over']"
                  v-if="row.activityStatus < 4 && row.activityStatus > 1"
                  >提前结束</el-dropdown-item
                >
                <el-dropdown-item
                  command="handleActivityData"
                  icon="el-icon-search"
                  v-hasPermi="['activityManage:activityManage:activityData']"
                  v-if="row.activityStatus > 1"
                  >活动数据</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
        <el-table-column
          label="创建日期"
          align="center"
          min-width="160"
          show-overflow-tooltip
          prop="createTime"
        />
        <el-table-column
          label="活动类型"
          align="center"
          min-width="120"
          show-overflow-tooltip
          prop="activityType"
        >
          <template slot-scope="scope">
            {{ scope.row.activityType | typeFilter(dict.type.activityType) }}
          </template>
        </el-table-column>
        <el-table-column
          label="活动模板"
          align="center"
          min-width="120"
          show-overflow-tooltip
          prop="templateName"
        >
          <template slot-scope="scope">
            <span
              v-if="
                scope.row.templateType == 1 &&
                scope.row.templateClassification == 1
              "
              ><dict-tag
                :options="dict.type.coupon_template"
                :value="scope.row.templateName"
            /></span>
            <span
              v-if="
                scope.row.templateType == 2 &&
                scope.row.templateClassification == 2
              "
              ><dict-tag
                :options="dict.type.model_template"
                :value="scope.row.templateName"
            /></span>
            <span
              v-if="
                scope.row.templateType == 1 &&
                scope.row.templateClassification == 3
              "
              ><dict-tag
                :options="dict.type.holiday_coupon_template"
                :value="scope.row.templateName"
            /></span>
            <span
              v-if="
                scope.row.templateType == 2 &&
                scope.row.templateClassification == 3
              "
              ><dict-tag
                :options="dict.type.holiday_model_template"
                :value="scope.row.templateName"
            /></span>
          </template>
        </el-table-column>
        <el-table-column
          label="活动名称"
          align="center"
          min-width="120"
          show-overflow-tooltip
          prop="activityName"
        />
        <el-table-column
          label="活动状态"
          align="center"
          min-width="120"
          show-overflow-tooltip
          prop="activityStatus"
        >
          <template slot-scope="scope">
            {{
              scope.row.activityStatus | typeFilter(dict.type.activity_status)
            }}
          </template>
        </el-table-column>
        <el-table-column
          label="活动时间"
          align="center"
          min-width="260"
          show-overflow-tooltip
          prop="startTime"
        >
          <template slot-scope="scope">
            <span>
              开始时间:{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}
            </span>
            <span>
              结束时间:{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="活动创建人"
          align="center"
          min-width="120"
          show-overflow-tooltip
          prop="createByName"
        />
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import {
  listActivityManage,
  getActivityManage,
  delActivityManage,
  suspendActivityManage,
  recoveryActivityManage,
  pushActivityManage,
  overActivityManage,
  exportActivityManage,
} from '@/api/marketingManagement/activityManage'

export default {
  name: 'ActivityManage',
  dicts: [
    'sys_business',
    'activityType',
    'activity_status',
    'coupon_template',
    'model_template',
    'holiday_coupon_template',
    'holiday_model_template',
  ],
  data() {
    return {
      marketingTarget: 0,
      activityType: null,
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 营销活动表格数据
      activityManageList: [],
      time: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        activityType: null,
        templateDictType: null,
        activityName: null,
        startTime: null,
        endTime: null,
        activityStatus: null,
      },
    }
  },
  filters: {
    typeFilter(val, type) {
      if (val == null) return null
      let data = type.filter((item) => {
        return val == item.value
      })
      if (data.length) {
        return data[0].label
      } else {
        return val
      }
    },
  },
  created() {
    let marketingTarget = this.$route.query.target
    this.marketingTarget = marketingTarget
    this.getList()
  },
  methods: {
    /** 查询营销活动列表 */
    getList() {
      this.loading = true
      this.queryParams.marketingTarget = this.marketingTarget
      listActivityManage(this.queryParams).then((response) => {
        this.activityManageList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 更多操作 */
    handleCommand(command, row) {
      switch (command) {
        case 'handleUpdate':
          this.handleUpdate(row)
          break
        case 'handleDelete':
          this.handleDelete(row)
          break
        case 'handleSuspend':
          this.handleSuspend(row)
          break
        case 'handleStart':
          this.handleStart(row)
          break
        case 'handleRecovery':
          this.handleRecovery(row)
          break
        case 'handlePush':
          this.handlePush(row)
          break
        case 'handleOver':
          this.handleOver(row)
          break
        case 'handleActivityData':
          this.handleActivityData(row)
          break
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        name: 'activityManageEdit',
        query: { id: row.activityId, type: row.activityType },
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const activityIds = row.activityId || this.ids
      const activityName = row.activityName
      this.$modal
        .confirm('是否确认删除营销活动  [' + activityName + ']  ？')
        .then(function () {
          return delActivityManage(activityIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 开始 */
    handleStart(row) {
      const activityId = row.activityId
      const activityName = row.activityName
      this.$modal
        .confirm('是否确认开始营销活动  [' + activityName + ']  吗？')
        .then(function () {
          return recoveryActivityManage(activityId)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('开始成功')
        })
        .catch(() => {})
    },
    /** 暂停 */
    handleSuspend(row) {
      const activityId = row.activityId
      const activityName = row.activityName
      this.$modal
        .confirm('是否确认暂停营销活动  [' + activityName + ']  ？')
        .then(function () {
          return suspendActivityManage(activityId)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('暂停成功')
        })
        .catch(() => {})
    },
    /** 恢复 */
    handleRecovery(row) {
      const activityId = row.activityId
      const activityName = row.activityName
      this.$modal
        .confirm('是否确认恢复营销活动  [' + activityName + ']  ？')
        .then(function () {
          return recoveryActivityManage(activityId)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('恢复成功')
        })
        .catch(() => {})
    },
    /** 精准推送 */
    handlePush(row) {
      const activityId = row.activityId
      const activityName = row.activityName
      this.$modal
        .confirm('是否确认推送营销活动  [' + activityName + ']  ？')
        .then(function () {
          return pushActivityManage(activityId)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('推送成功')
        })
        .catch(() => {})
    },
    /** 提前结束 */
    handleOver(row) {
      const activityId = row.activityId
      const activityName = row.activityName
      this.$modal
        .confirm('是否确认提前结束营销活动  [' + activityName + ']  ？')
        .then(function () {
          return overActivityManage(activityId)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('结束成功')
        })
        .catch(() => {})
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.time) {
        this.queryParams.startTime = this.time[0]
        this.queryParams.endTime = this.time[1]
      } else {
        this.queryParams.startTime = null
        this.queryParams.endTime = null
      }
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.time = []
      this.resetForm('queryForm')
      this.handleQuery()
    },

    /** 活动数据 */
    handleActivityData(row) {
      this.$router.push({
        name: 'activityManageData',
        query: { id: row.activityId, type: row.activityType },
      })
    },
    /** 活动详情 */
    getInfo(row) {
      this.$router.push({
        name: 'activityManageInfo',
        query: { id: row.activityId, type: row.activityType },
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal
        .confirm('是否确认导出所有营销活动数据项？')
        .then(() => {
          this.exportLoading = true
          return exportActivityManage(queryParams)
        })
        .then((response) => {
          this.$download.name(response.msg)
          this.exportLoading = false
        })
        .catch(() => {})
    },
  },
}
</script>
